package com.example.footprint;

import java.util.List;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.google.android.gcm.GCMRegistrar;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MainActivity extends MapActivity {

	private MapView mapView;
	private MapController mymapController;
	private GeoPoint myCurrentPoint;
	private List<Overlay> mapOverlays;
	private MessagesOverlay msmOverlay;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button msmBtn = (Button) findViewById(R.id.messagesbtn);
        msmBtn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MainActivity.this, MessageDisplayActivity.class);
				startActivity(intent);
			}
		});
        
//        preparing work
        mapView = (MapView) findViewById(R.id.mapview);
        mapOverlays = mapView.getOverlays();
        mymapController = mapView.getController();
//        enable zooming 
        mapView.setBuiltInZoomControls(true);
        mymapController.setZoom(9);
//        set displayed type --satellite view
        mapView.setSatellite(true);
       	mapView.setEnabled(true);
      
//        get current location
        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener locationListener = new myLocationListener();
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 5, locationListener);
       	
       	
//        test MessageOverlay and MessageItem
       	Double lat = 42.0244797 * 1E6;
       	Double lng = -93.6357134 * 1E6;
       	GeoPoint iowaStatePoint = new GeoPoint(lat.intValue(), lng.intValue());
//       	mymapController.setCenter(iowaStatePoint);
        Drawable drawable = this.getResources().getDrawable(R.drawable.message);
        msmOverlay = new MessagesOverlay(drawable, this);
        MessageItem msmItem = new MessageItem(iowaStatePoint, "Iowa State University", "This is ISU");
        msmOverlay.addItem(msmItem);
        mapOverlays.add(msmOverlay);
        
        
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	private class myLocationListener implements LocationListener {

		public void onLocationChanged(Location loc) {
			// TODO Auto-generated method stub
			if(loc != null) {
				myCurrentPoint = new GeoPoint((int)(loc.getLatitude() * 1E6),(int)(loc.getLongitude() * 1E6));
				// go to aimed point by animating
				mymapController.animateTo(myCurrentPoint);
				// jump to aimed point directly
				// mymapController.setCenter(myCurrentPoint);
				addGPSLayer();
			}
		}

		public void onProviderDisabled(String arg0) {
			// TODO Auto-generated method stub
			
		}

		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
			
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
			
		}
	}
	
	public void addGPSLayer() {
		List<Overlay> mapOverlays = mapView.getOverlays();
		Drawable drawable = this.getResources().getDrawable(R.drawable.current_location);
		
		MyItemizedOverlay currentLocOverlay = new MyItemizedOverlay(drawable, this);
		OverlayItem newItem = new OverlayItem(myCurrentPoint, "Hello", "I'm here!");
		currentLocOverlay.addOverlay(newItem);
		
		mapOverlays.clear();
		mapOverlays.add(currentLocOverlay);
		mapOverlays.add(msmOverlay);
		
	}
}
